package com.mx.kn.cstf2.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import kn.cstf2.transition.Bean.TransitionBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mx.kn.cstf2.dao.CopDao;
import com.mx.kn.cstf2.dao.TransitionDao;
import com.mx.kn.cstf2.dto.ClientesSTKDto;
import com.mx.kn.cstf2.dto.CopAttachmentsDto;
import com.mx.kn.cstf2.dto.CopContactsDto;
import com.mx.kn.cstf2.dto.CopDto;
import com.mx.kn.cstf2.dto.CopWorkingInstructionsDto;
import com.mx.kn.cstf2.dto.CustomerSolutionDto;
import com.mx.kn.cstf2.dto.ExceptionCodeDto;
import com.mx.kn.cstf2.dto.KPIFieldsDto;
import com.mx.kn.cstf2.dto.UserClientsSTKDto;
import com.mx.kn.cstf2.dto.UsuarioDto;
import com.mx.kn.cstf2.service.CopService;
import com.mx.kn.cstf2.service.TransitionService;

@Service
public class CopServiceImpl implements CopService{
	@Autowired TransitionDao transitionDao;
	@Autowired CopDao copDao;

	@Override
	public TransitionBean updateReportingTransition(TransitionBean transition)
			throws SQLException {
		copDao.updateReportingTransition(transition);
		return transitionDao.obtenerTransitionBeanByKnkeyAndId(transition);
	}

	@Override
	public TransitionBean updateScopeOfServiceTransition(TransitionBean transition)
			throws SQLException {
		copDao.updateScopeOfServiceTransition(transition);
		return transitionDao.obtenerTransitionBeanByKnkeyAndId(transition);
	}

	@Override
	public List<CopDto> getImports(TransitionBean transition)
			throws SQLException {
		return copDao.getImports(transition);
	}

	@Override
	public void addImport(TransitionBean transition, UsuarioDto usuario, Date fecha) throws SQLException {
		
		copDao.addImport(transition, usuario, fecha);
	}

	@Override
	public List<CopDto> getExports(TransitionBean transition)
			throws SQLException {
		return copDao.getExports(transition);
	}

	@Override
	public void addExport(TransitionBean transition, UsuarioDto usuario, Date fecha) throws SQLException {
		copDao.addExport(transition, usuario, fecha);
		
	}

	@Override
	public void updateCop(CopDto datos) throws SQLException {
		copDao.updateCop(datos);
		
	}

	@Override
	public CopDto getForm(Integer id) throws SQLException {
		List<CopDto> datos = copDao.getForm(id);
		if(datos.size()>0){
			for(CopDto formulario : datos){
				return formulario;
			}			
		}	
		return null;
	}

	@Override
	public List<CopContactsDto> getContactsList(CopDto copDto)
			throws SQLException {
		
		return copDao.getContactsList(copDto);
	}

	@Override
	public ClientesSTKDto getClientInfo(Integer knkey)
			throws SQLException {
		List<ClientesSTKDto> datos = copDao.getClientInfo(knkey);
		
		if(datos.size()>0){
			for(ClientesSTKDto infoCliente : datos){
				return infoCliente;
			}
		}
		
		return null;
	}

	@Override
	public ArrayList<String> getBillingInstructions(Integer knkey)
			throws SQLException {
		List<UserClientsSTKDto> billInst = copDao.getBillingInstructions(knkey);
		ArrayList<String> contenido = new ArrayList<String>();
		
		if(billInst.size()>0){
			for(UserClientsSTKDto infoCliente : billInst){
				contenido.add(infoCliente.getWinuser());
			}
		}
		return contenido;
	}

	@Override
	public void addContact(CopContactsDto contact) throws SQLException {
		copDao.addContact(contact);
		
	}

	@Override
	public void addExcetionCode(ExceptionCodeDto exception) throws SQLException {
		copDao.addExcetionCode(exception);
		
	}

	@Override
	public List<ExceptionCodeDto> getExceptionsByIdCop(
			ExceptionCodeDto exception) throws SQLException {
		return copDao.getExceptionsByIdCop(exception);
	}

	@Override
	public CustomerSolutionDto getReports(Integer knkey) throws SQLException {
		List<CustomerSolutionDto> reportes = copDao.getReports(knkey);
		
		if(reportes.size()>0){
			for(CustomerSolutionDto infoReports : reportes){
				return infoReports;
			}
		}
		return null;
	}

	@Override
	public List<KPIFieldsDto> getKPIFields(Integer idCop) throws SQLException {
		return copDao.getKPIFields(idCop);
	}

	@Override
	public void updateKPIFields(KPIFieldsDto updateKPI)
			throws SQLException {
		copDao.updateKPIFields(updateKPI);		
	}

	@Override
	public void updateKPIChecks(KPIFieldsDto updateKPI) throws SQLException {
		copDao.updateKPIChecks(updateKPI);
		
	}

	@Override
	public void insertKPI(KPIFieldsDto insertKPI) throws SQLException {
		copDao.insertKPI(insertKPI);
		
	}

	@Override
	public List<CopWorkingInstructionsDto> getWorkInstFields(
			Integer tipoModulo, Integer idCop) throws SQLException {
		
//		List<CopWorkingInstructionsDto> fieldsAndValues = copDao.getWorkInstFields(tipoModulo, idCop);
//		
//		if(fieldsAndValues.size()<1)
//			fieldsAndValues = copDao.getWorkInstFieldsSecCase(tipoModulo, idCop);	
			
//		return copDao.getWorkInstFieldsSecCase(tipoModulo, idCop);	
		return copDao.getWorkInstFields(tipoModulo, idCop);
	}

	@Override
	public void updateWorkInstFields(Integer idWorkInst, String textValue, Integer idCop)
			throws SQLException {
		copDao.updateWorkInstFields(idWorkInst, textValue, idCop);
		
	}

	@Override
	public int getconsecutiveNameFile() throws SQLException {
		return copDao.getconsecutiveNameFile();
	}

	@Override
	public void saveNameAttachment(CopAttachmentsDto attachment)
			throws SQLException {
		copDao.saveNameAttachment(attachment);
	}

	@Override
	public List<CopAttachmentsDto> getAttachmentName(
			CopAttachmentsDto attachment) throws SQLException {
		return copDao.getAttachmentName(attachment);
	}

	@Override
	public void deleteAttachment(CopAttachmentsDto attachment)
			throws SQLException {
		copDao.deleteAttachment(attachment);
		
	}

	@Override
	public void deleteContactCop(CopContactsDto contact) throws SQLException {
		copDao.deleteContactCop(contact);
		
	}

	@Override
	public List<CopContactsDto> getContactsListMail(long idTransition)
			throws SQLException {
		return copDao.getContactsListMail(idTransition);
	}

	@Override
	public boolean validaExistenciaCop(Integer idCop) throws SQLException {
		if(copDao.validaExistenciaCop(idCop).size()>0)
			return true;
		else
			return false;				
	}

	@Override
	public void insertWorkInstFields(Integer idWorkInst,
			Integer idCop) throws SQLException {
		copDao.insertWorkInstFields(idWorkInst, idCop);
		
	}

	@Override
	public List<CopWorkingInstructionsDto> getWorkInstFieldsSecCase(
			Integer tipoModulo) throws SQLException {
		// TODO Auto-generated method stub
		return copDao.getWorkInstFieldsSecCase(tipoModulo);
	}

}
